package cn.hxsteel.storage.module.goods;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hxsteel.jpart.pager.PagerVO;
import cn.hxsteel.model.bo.ViewBO;
import cn.hxsteel.storage.core.base.BaseController;
import cn.hxsteel.storage.model.bo.Depot2BO;
import cn.hxsteel.storage.model.po.DepotPO;
import cn.hxsteel.storage.model.po.GoodsPO;
import cn.hxsteel.storage.module.goods.model.DepotBO;
import cn.hxsteel.storage.module.goods.model.GoodsBO;

/**
 * 商品的控制器
 */
@Controller
public class GoodsController extends BaseController
{
    @Autowired
    private GoodsService goodsService;
    
    /**
     * 商品管理的主视图
     */
    @RequestMapping( "/goodsManage" )
    public ModelAndView goodsManageView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	String[] uriArray = new String[] { "/goods", "/depot" };
	String viewName = getUserPermitViewName( uriArray, request );
	res.setViewName( viewName );
	return res;
    }
    
    /**
     * 返回添加商品视图
     * 
     * @param name 商品名
     * @param material 材质
     * @param origin 产地
     * @return
     */
    @RequestMapping( "/goods/add" )
    public ModelAndView addView( String name, String material, String origin )
    {
	ModelAndView mav = new ModelAndView();
	mav.addObject( "name", name );
	mav.addObject( "material", material );
	mav.addObject( "origin", origin );
	mav.setViewName( "/goods/goods/add" );
	return mav;
    }
    
    /**
     * 验证商品名材质产地规格是否已经存在
     * 
     * @param name 商品名
     * @param material 材质
     * @param origin 产地
     * @param norm 规格
     * @return
     */
    @RequestMapping( "/goods/check" )
    @ResponseBody
    public String checkgoods( String name, String material, String origin, String norm )
    {
	String msg;
	if ( goodsService.existGoods( name, material, origin, norm ) == false )
	{
	    msg = "-1";
	    return msg;
	}
	else
	{
	    msg = "0";
	    return msg;
	}
    }
    
    /**
     * 选择进入的视图
     * 
     * @return
     */
    @RequestMapping( "/goods" )
    public ModelAndView choosegoodsview()
    {
	ModelAndView mav = new ModelAndView();
	if ( goodsService.existGoods1() == false )
	{
	    mav.setViewName( getRedirectUrl( "/goods/add?name=&material=&origin=" ) );
	}
	else
	{
	    mav.addObject( "goodslist", goodsService.goodsList() );
	    mav.setViewName( "/goods/goods/search" );
	}
	return mav;
    }
    
    /**
     * 添加商品操作
     * 
     * @param goodsPO 商品的持久化类
     * @return
     */
    @RequestMapping( "/goods/add/do" )
    public ModelAndView addgoods( GoodsPO goodsPO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/goods/goods/search" );
	try
	{
	    validator.validate( goodsPO );
	    goodsService.addgoods( goodsPO );
	}
	catch ( Exception e )
	{
	    res.setViewName( "goods/goods/add" );
	}
	return res;
    }
    
    /**
     * 返回修改商品视图
     * 
     * @param id 商品ID
     * @return
     */
    @RequestMapping( "/goods/edit" )
    public ModelAndView updgoodsview( int id )
    {
	ModelAndView mav = new ModelAndView();
	mav.addObject( "goods", goodsService.goodsByID( id ) );
	mav.setViewName( "/goods/goods/edit" );
	return mav;
    }
    
    /**
     * 修改商品属性
     * 
     * @param goodsPO 商品的持久化类
     * @return
     */
    @RequestMapping( "/goods/edit/do" )
    public ModelAndView updgoods( GoodsPO goodsPO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "goods/goods/search" );
	try
	{
	    validator.validate( goodsPO );
	    goodsService.updgoods( goodsPO );
	}
	catch ( Exception e )
	{
	    res.setViewName( "goods/goods/edit" );
	}
	return res;
    }
    
    /**
     * 验证商品的各个属性是否存在
     * 
     * @param id 商品ID
     * @param name 商品名
     * @param material 材质
     * @param origin 产地
     * @param norm 规格
     * @return
     */
    @RequestMapping( "/goods/checkByall" )
    @ResponseBody
    public String checkgoodsall( int id, String name, String material, String origin, String norm )
    {
	String msg;
	if ( goodsService.existGoods( name, material, origin, norm ) == false )
	{
	    msg = "-1";
	}
	else
	{
	    msg = "0";
	}
	return msg;
    }
    
    /**
     * 通过商品名称查询商品材质
     * 
     * @param name 商品名称
     * @return
     */
    @RequestMapping( "/goods/selgoodsByname" )
    @ResponseBody
    public List< GoodsPO > selgoodByname( String name )
    {
	List< GoodsPO > goodslist = goodsService.goodsListByname( name );
	return goodslist;
    }
    
    /**
     * 通过商品名称和商品材质查询产地
     * 
     * @param name 商品名称
     * @param material 商品材质
     * @return
     */
    @RequestMapping( "/goods/selgoodsByNameAndMaterial" )
    @ResponseBody
    public List< GoodsPO > selgoodByNameAndMaterial( String name, String material )
    {
	List< GoodsPO > goodslist = goodsService.goodsListByNameAndMaterial( name, material );
	return goodslist;
    }
    
    /**
     * 列出商品名称
     * 
     * @return
     */
    @RequestMapping( "/goods/selgoods" )
    @ResponseBody
    public List< GoodsPO > selgoodsall()
    {
	List< GoodsPO > goodslist = goodsService.goodsList();
	return goodslist;
    }
    
    /**
     * 分页查询
     * 
     * @param page 页码
     * @param pageSize 每页显示多少条信息
     * @param name 商品名
     * @param material 材质
     * @param origin 产地
     * @return
     */
    @RequestMapping( "/goods/s/do" )
    @ResponseBody
    public ViewBO selgoodspage( int pageNo, int pageSize, String name, String material, String origin )
    {
	ViewBO pagerbo = new ViewBO();
	List< GoodsBO > goodslist = goodsService.goodsPage( pageNo, pageSize, name, material, origin );
	PagerVO pagerVO = goodsService.createPagerData();
	pagerbo.put( "name", name );
	pagerbo.put( "material", material );
	pagerbo.put( "origin", origin );
	pagerbo.putData( goodslist );
	pagerbo.putPager( pagerVO );
	return pagerbo;
    }
    
    /**
     * 选择跳转界面
     * 
     * @return
     */
    @RequestMapping( "/depot" )
    public ModelAndView choosedepotview()
    {
	ModelAndView mav = new ModelAndView();
	if ( goodsService.existDepot() == false )
	{
	    mav.setViewName( "/goods/depot/add" );
	}
	else
	{
	    mav.setViewName( "/goods/depot/search" );
	}
	return mav;
    }
    
    /**
     * 分页查询仓库
     * 
     * @param page 页码
     * @param pageSize 每页多少条记录
     * @return
     */
    @RequestMapping( "/depot/s/do" )
    @ResponseBody
    public ViewBO seldepotpage( int pageNo, int pageSize )
    {
	ViewBO pagebo = new ViewBO();
	List< DepotBO > depotlist = goodsService.depotPage( pageNo, pageSize );
	PagerVO pagerVO = goodsService.createPagerData();
	pagebo.putData( depotlist );
	pagebo.putPager( pagerVO );
	return pagebo;
    }
    
    /**
     * 通过仓库ID返回修改视图
     * 
     * @param id 仓库ID
     * @return
     */
    @RequestMapping( "/depot/edit/id" )
    public ModelAndView upddepotview( int id )
    {
	ModelAndView mav = new ModelAndView();
	mav.addObject( "depotPO", goodsService.selDepotByID( id ) );
	mav.setViewName( "/goods/depot/edit" );
	return mav;
    }
    
    /**
     * 通过父仓库name返回修改视图
     * 
     * @param name 父仓库名
     * @return
     */
    @RequestMapping( "/depot/edit/name" )
    public ModelAndView upddepotview( String name )
    {
	ModelAndView mav = new ModelAndView();
	mav.addObject( "depotPO", goodsService.selDepotByName( name ) );
	mav.setViewName( "/goods/depot/edit" );
	return mav;
    }
    
    /**
     * 修改操作验证仓库名称是否存在
     * 
     * @param id 仓库ID
     * @param pid 仓库PID
     * @param name 仓库name
     * @return
     */
    @RequestMapping( value = "/valid/depot/name1/do" )
    public @ResponseBody
    boolean validdepot1( int id, int pid, String name )
    {
	return !goodsService.existdepotBy1( id, pid, name );
    }
    
    /**
     * 修改仓库属性
     * 
     * @param depotPO 商品的持久化类
     * @return
     */
    @RequestMapping( "/depot/edit/do" )
    public ModelAndView upddepot( DepotPO depotPO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "goods/depot/search" );
	try
	{
	    validator.validate( depotPO );
	    goodsService.upddepot( depotPO );
	}
	catch ( Exception e )
	{
	    res.setViewName( "goods/depot/edit" );
	}
	return res;
    }
    
    /**
     * 通过父仓库name返回添加视图
     * 
     * @param name 父仓库名
     * @return
     */
    @RequestMapping( "/depot/add/name" )
    public ModelAndView adddepotview( String name )
    {
	ModelAndView mav = new ModelAndView();
	mav.addObject( "pid", goodsService.selDepotByName( name ).getPid() );
	mav.setViewName( "/goods/depot/add" );
	return mav;
    }
    
    /**
     * 通过仓库ID返回添加视图
     * 
     * @param id 仓库ID
     * @return
     */
    @RequestMapping( "/depot/add/id" )
    public ModelAndView adddepotview( int id )
    {
	ModelAndView mav = new ModelAndView();
	if ( id == 0 )
	{
	    mav.addObject( "pid", -1 );
	}
	else
	{
	    mav.addObject( "pid", goodsService.selDepotByID( id ).getPid() );
	}
	System.out.println( goodsService.depotList() );
	mav.addObject( "depotlist", goodsService.depotList() );
	mav.setViewName( "/goods/depot/add" );
	return mav;
    }
    
    /**
     * 添加操作验证仓库名称是否存在
     * 
     * @param pid 仓库PID
     * @param name 仓库name
     * @return
     */
    @RequestMapping( value = "/valid/depot/name/do" )
    public @ResponseBody
    boolean validdepot( int pid, String name )
    {
	return !goodsService.existdepotByNameAndPID( pid, name );
    }
    
    /**
     * 添加仓库属性
     * 
     * @param depotPO 商品的持久化类
     * @return
     */
    @RequestMapping( "/depot/add/do" )
    public ModelAndView adddepot( DepotPO depotPO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "goods/depot/search" );
	try
	{
	    validator.validate( depotPO );
	    goodsService.adddepot( depotPO );
	}
	catch ( Exception e )
	{
	    res.setViewName( "goods/depot/add" );
	}
	return res;
    }
    
    /**
     * 跳转到录入基础数据
     * 
     * @param id 仓库ID
     * @return
     */
    @RequestMapping( "/depot/enter" )
    public ModelAndView enterdepot( int id )
    {
	ModelAndView mav = new ModelAndView();
	mav.setViewName( "/goods/depot/search1" );
	mav.addObject( "depotid", id );
	return mav;
    }
    
    /**
     * 录入基础数据分页
     * 
     * @param pageNo 页码
     * @param pageSize 每页多少条记录
     * @param depotid 仓库ID
     * @return
     */
    @RequestMapping( "/depot/s1/do" )
    @ResponseBody
    public ViewBO seldepotpage1( int pageNo, int pageSize, int depotid )
    {
	ViewBO pagebo = new ViewBO();
	List< Depot2BO > depotlist = goodsService.depotPage1( pageNo, pageSize, depotid );
	PagerVO pagerVO = goodsService.createPagerData();
	pagebo.putData( depotlist );
	pagebo.putPager( pagerVO );
	return pagebo;
    }
}